class Solution(object):
    def firstUniqChar(self, s):
        """
        :type s: str
        :rtype: int
        """
        # 法一：时间复杂度较高O(n^2)
        # for i, alpha in enumerate(s):
        #     if alpha not in s[i+1:] and alpha not in s[:i]:
        #         return i
        # return -1
        # 法二：利用字典统计字符出现次数，时间复杂度O(n)
        freq = {}
        for c in s:
            if c not in freq:
                freq[c] = 1
            else:
                freq[c] += 1
        # # 可以简化写
        # for c in s:
        #     freq[c] = freq.get(c, 0) + 1 # 如果 c 不在字典中，get(c, 0) 返回 0，结果就是 1;如果 c 已存在，返回当前次数，加 1
        for i, c in enumerate(s):
            if freq[c] == 1:
                return i
        return -1

if __name__ == '__main__':
    s = "leetcode"
    print(Solution().firstUniqChar(s))